package main.iwith;

import main.util.JsonUtil;
import main.util.WebConn;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Operator {
	
	public static Sheet sheet;

	public static double SUCESS = 0;

	public static double FAIL = 0;

	public static int NUM = 0;

	public static String STR = "";

	public static void main(String[] args) throws IOException {
		
		Workbook wb=null;
		FileOutputStream fos=null;
		try {
			wb = new HSSFWorkbook();
			sheet = wb.createSheet();
			
			//开始生成测试表格
			process();

			// 生成识别错误的语句(可以识别但是识别错误)
			fos=new FileOutputStream("G:/SB1.xls");
			wb.write(fos);

		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			if(wb!=null){
				wb.close();
			}
			
			if(fos!=null){
				fos.close();
			}

			System.out.println("识别成功率");
			System.out.println(SUCESS/(SUCESS + FAIL));

			// 生成识别不了的语句
			File file = new File("G:/sb_name.txt");
			if (file.exists()) {
				file.delete();
			}
			file.createNewFile();

			BufferedWriter writer = new BufferedWriter(new FileWriter(file));
			String[] strings = STR.substring(0, STR.length() - 1).split(",");
			for(int i = 0; i < strings.length; i++){
				if(strings[i].toString().length() > 0){
					// 生成识别不了语句的格式（可修改）
					writer.write(strings[i].toString() + System.getProperty("line.separator"));
				}

			}
			//关闭流
			writer.close();

		}
	}

	
	public static void process(){
		FileReader file;
		try {
			file = new FileReader("G:/SB.txt");
			BufferedReader br=new BufferedReader(file);
	        Map<String,Object> map=new HashMap<String,Object>();
	        String line ;
//	        int rowNum=0;
	        while ((line = br.readLine())!=null){
	            String name=ForAnligla.getItemName(line);
				if(HasDigit(name)){
					for(int i = 0; i < name.length(); i++){
						if(isDigit(name.charAt(i))){
							name = name.substring(0, i);
							break;
						}
					}
				}

				map=getIntend(name);
	            
//	            Long time=(Long)map.get("time");
//	            rowNum++;

				//todo intends参数是正确的意图命令
	            ForAnligla.fillRow(sheet,map,NUM,name,"order.ask.item.name");
	            /*if(!"order.ask.name.att".equals(map.get("intentName").toString()) || time>5000){
	            	
	            	System.out.println("......***error end "+name);
	            	System.out.println("......***error line:"+line);
	    		}else{
	    			//System.out.println("......success end "+name);
	    		}*/
	        }
	        
	        
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static boolean HasDigit(String content) {
		boolean flag = false;
		Pattern p = Pattern.compile(".*\\d+.*");
		Matcher m = p.matcher(content);
		if (m.matches()) {
			flag = true;
		}
		return flag;
	}
	
	public static final String url = "http://47.74.1.189:8089/nlg/dialog?txt=";
	public static Map<String,Object> getIntend(String name){
		Map<String,Object> map=new HashMap<String, Object>();
		
		long start=System.currentTimeMillis();
		try {
			String param=URLEncoder.encode(name);
			String obj=WebConn.doGet(url + param);
			Map pmap=JsonUtil.convertJson2Object(obj, Map.class);
			map = (Map<String,Object>)pmap.get("entry");
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("******error:"+name);
		}
		long end=System.currentTimeMillis();
		
		map.put("time", end-start);
		
		return map;
	}

	public static boolean isDigit(char ch){
		if(ch >= 48 && ch <= 57){
			return true;
		}
		return false;
	}
}
